package cc.rengu.igas.cops.core.service.trans.unionpay;

import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.cops.facade.request.CreateUserAuthCodeRequest;
import cc.rengu.igas.cops.facade.response.CreateUserAuthCodeRespsonse;
import cc.rengu.igas.share.core.model.UserInfo;
import cc.rengu.igas.share.core.realize.UserService;
import cc.rengu.igas.share.core.realize.impl.UserServiceImpl;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.utility.util.*;


/**
 * 银联与微信条码互通功能
 * 生成用户授权code
 *
 * @author panky
 * @version 1.0.0 创建时间：2020/08/14
 */
public class CreateUserAuthCodeService extends BizBaseService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CreateUserAuthCodeRequest createUserAuthCodeRequest = new CreateUserAuthCodeRequest();
        ConvertUtil.convertOutput(createUserAuthCodeRequest);
        return createUserAuthCodeRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        CreateUserAuthCodeRequest createUserAuthCodeRequest = (CreateUserAuthCodeRequest) request;

        if (StringUtil.isEmptyOrNull(createUserAuthCodeRequest.getJwToken())) {
            BizResponse response = new BizResponse();
            response.setRspSysId(AppParamConstant.IGAS);
            response.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespCode());
            response.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespDesc());
            return response;
        }

        //解码jwToken
        String cstNo = "";
//        Map<String, Claim> resultMap = JwTokenDecryptUtil.verifyMobileToken(createUserAuthCodeRequest.getJwToken());
//        if(resultMap != null && resultMap.get("cstNo") != null){
//            Claim cstNoClaim = resultMap.get("cstNo");
//            cstNo = cstNoClaim.asString();
//        } else {
//            rglog.error("JWT解析失败");
//            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
//        }

        // 根据核心客户号查询出用户编号
        // 核心客户号查询用户
        UserService userService = new UserServiceImpl();
        UserInfo userInfo = userService.getUserInfo(createUserAuthCodeRequest.getHeader().getInstId(), cstNo, createUserAuthCodeRequest.getHeader().getSession());

        //生成userAuthCode
        String userAuthCode = createUserAuthCodeRequest.getHeader().getInstId() + MD5Util.MD5Encode(userInfo.getUserId() + RandomUtil.getRandomStr(4));
        //将userAuthCode放在redis中，有效期5分钟
        String key = "union_wechat_qr_" + userAuthCode;
        RedisUtil.onceSet(key, userAuthCode, 300);

        CreateUserAuthCodeRespsonse createUserAuthCodeRespsonse = new CreateUserAuthCodeRespsonse();
        createUserAuthCodeRespsonse.setUserAuthCode(userAuthCode);

        rglog.info("生成userAuthCode成功，instId:<{}>, userAutCode:<{}>, redis_key:<{}>!", createUserAuthCodeRequest.getHeader().getInstId(), createUserAuthCodeRespsonse.getUserAuthCode(), key);

        BizResponse response = new BizResponse();
        response.setResult(createUserAuthCodeRespsonse);
        response.setRspSysId(AppParamConstant.IGAS);
        response.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        response.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return response;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        CreateUserAuthCodeRespsonse createUserAuthCodeRespsonse = (CreateUserAuthCodeRespsonse) response.getResult();
        ConvertUtil.convertInput(createUserAuthCodeRespsonse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
